<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <base href="<%=basePath%>">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>

    <script type="text/javascript">

        //页面加载完毕
        $(function () {
            pageList();
            $("#qx").click(function () {

                $("input[name=xz]").prop("checked", this.checked);

            })
            $("#dicValueBody").on("click", $("input[name=xz]"), function () {

                $("#qx").prop("checked", $("input[name=xz]").length == $("input[name=xz]:checked").length);

            })

            $("#addBtn").click(function () {

                $.ajax({

                    url: "settings/user/typeList.do",
                    data: {

                        "typeCode": "dicType"

                    },
                    type: "get",
                    dataType: "json",
                    success: function (data) {

						var dicType = "${typeCode2}";

						//alert(dicType);

                        $("#create-typeCode2").val(dicType);
						var html = "<option></option>";

						$.each(data, function (i, n) {

							html += "<option value='" + n.code + "'>" + n.code + "</option>";

						})

						$("#create-typeCode").html(html);

                        //处理完所有者下拉框数据后，打开模态窗口
                        $("#createDicValueModal").modal("show");

                    }

                })

            })

            $("#saveBtn").click(function () {

                $.ajax({

                    url: "settings/user/save.do",
                    data: {

                        "value": $.trim($("#create-value").val()),
                        "text": $.trim($("#create-text").val()),
                        "orderNo": $.trim($("#create-orderNo").val()),
                        "typeCode": $.trim($("#create-typeCode").val()),
                        "typeDic": "dicValue"

                    },
                    type: "post",
                    dataType: "json",
                    success: function (data) {

                        if (data.success) {

                            var flag = $("#flag").val();
                            if (flag != "1") {

                                pageList();

                            } else {
                                window.location.href = window.location.href;
                                //pageList2();
                            }


                            $("#dicValueAddForm")[0].reset();
                            //关闭模态窗口
                            $("#createDicValueModal").modal("hide");

                        } else {

                            alert("添加字典值失败");

                        }

                    }

                })

            })

            //为删除按钮绑定事件，执行删除操作
            $("#deleteBtn").click(function () {
                var flag = $("#flag").val();
                //找到复选框中所有挑√的复选框的jquery对象
                var $xz = $("input[name=xz]:checked");

                if ($xz.length == 0) {

                    alert("请选择需要删除的记录");

                    //肯定选了，而且有可能是1条，有可能是多条
                } else {


                    if (confirm("确定删除所选中的记录吗？")) {

                        //拼接参数
                        var param = "";

                        //将$xz中的每一个dom对象遍历出来，取其value值，就相当于取得了需要删除的记录的id
                        for (var i = 0; i < $xz.length; i++) {

                            param += "id=" + $($xz[i]).val();

                            //如果不是最后一个元素，需要在后面追加一个&符
                            if (i < $xz.length - 1) {

                                param += "&";

                            }

                        }

                        $.ajax({

                            url: "settings/user/deleteByIds.do",
                            data: param,
                            type: "post",
                            dataType: "json",
                            success: function (data) {

                                if (data.success) {

                                    var flag = $("#flag").val();
                                    if (flag != "1") {

                                        pageList();

                                    } else {
                                        window.location.href = window.location.href;
                                    }

                                } else {

                                    alert("删除字典值失败");

                                }


                            }

                        })


                    }


                }


            })

            //为修改按钮绑定事件，打开修改操作的模态窗口
            $("#editBtn").click(function () {
                var flag = $("#flag").val();
                var $xz = $("input[name=xz]:checked");

                if ($xz.length == 0) {

                    alert("请选择需要修改的记录");

                } else if ($xz.length > 1) {

                    alert("只能选择一条记录进行修改");

                    //肯定只选了一条
                } else {

                    var id = $xz.val();

                    $.ajax({

                        url: "settings/user/getDicType.do",
                        data: {

                            "id": id,
                            "typeDic": "dicValue"
                        },
                        type: "get",
                        dataType: "json",
                        success: function (data) {

                            //处理单条
                            $("#edit-id").val(data.dv.id);
                            $("#edit-value").val(data.dv.value);
                            $("#edit-text").val(data.dv.text);
                            $("#edit-orderNo").val(data.dv.orderNo);
                            $("#edit-typeCode").val(data.dv.typeCode);

                            //所有的值都填写好之后，打开修改操作的模态窗口
                            $("#editDicValueModal").modal("show");

                        }

                    })

                }

            })

            //为更新按钮绑定事件，执行字典类型的修改操作
            $("#updateBtn").click(function () {
                var flag = $("#flag").val();
                $.ajax({

                    url: "settings/user/update.do",
                    data: {

                        "id": $.trim($("#edit-id").val()),
                        "value": $.trim($("#edit-value").val()),
                        "text": $.trim($("#edit-text").val()),
                        "orderNo": $.trim($("#edit-orderNo").val()),
                        "typeCode": $.trim($("#edit-typeCode").val()),
                        "typeDic": "dicValue"

                    },
                    type: "post",
                    dataType: "json",
                    success: function (data) {

                        if (data.success) {

                            var flag = $("#flag").val();
                            if (flag != "1") {

                                pageList();

                            } else {
                                window.location.href = window.location.href;
                            }

                            //关闭修改操作的模态窗口
                            $("#editDicValueModal").modal("hide");

                        } else {

                            alert("修改字典值失败");

                        }

                    }

                })

            })
        });

        function pageList() {
            var flag = $("#flag").val();

            if (flag != "1") {
                $.ajax({

                    url: "settings/user/typeList.do",
                    data: {

                        "typeCode": "dicValue"

                    },
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        //alert("进入ajax1");
                        var html = "";

                        //每一个n就是每一个市场活动对象
                        $.each(data, function (i, n) {

                            html += '<tr class="active">';
                            html += '<td><input type="checkbox" name="xz" value="' + n.id + '"/></td>';
                            html += '<td>' + (i + 1) + '</td>';
                            html += '<td>' + n.value + '</td>';
                            html += '<td>' + n.text + '</td>';
                            html += '<td>' + n.orderNo + '</td>';
                            html += '<td>' + n.typeCode + '</td>';
                            html += '</tr>';
                        })

                        $("#dicValueBody").html(html);
                    }

                })

            }


        }

        function pageList2() {
            var flag = $("#flag").val();

            if (flag == "0") {

                var typeCode2 = $("#typeCode2").val();

                $.ajax({

                    url: "settings/user/typeList.do",
                    data: {

                        "typeCode": typeCode2

                    },
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        //alert("进入ajax2");
                        var html = "";

                        //每一个n就是每一个市场活动对象
                        $.each(data, function (i, n) {

                            html += '<tr class="active">';
                            html += '<td><input type="checkbox" name="xz" value="' + n.id + '"/></td>';
                            html += '<td>' + (i + 1) + '</td>';
                            html += '<td>' + n.value + '</td>';
                            html += '<td>' + n.text + '</td>';
                            html += '<td>' + n.orderNo + '</td>';
                            html += '<td>' + n.typeCode + '</td>';
                            html += '</tr>';
                        })

                        $("#dicValueBody").html(html);
                    }

                })
            }


        }
    </script>
</head>
<body>

<!-- 创建字典值的模态窗口 -->
<div class="modal fade" id="createDicValueModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel1">新增字典值</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form" id="dicValueAddForm">
                    <div class="form-group">
                        <label for="create-typeCode" class="col-sm-2 control-label">字典类型编码<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">

                            <c:if test="${not empty typeCode2}">
                                <input type="text" class="form-control" id="create-typeCode2" style="width: 200%;" readonly>
                            </c:if>

							<c:if test="${empty typeCode2}">
                            <select class="form-control" id="create-typeCode" style="width: 200%;">
                                <option></option>
                                <c:forEach items="${dtList}" var="dl">
                                    <option value="${dl.code}">${dl.code}</option>
                                </c:forEach>
                            </select>
							</c:if>

                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-value" class="col-sm-2 control-label">字典值<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-value" style="width: 200%;">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-text" class="col-sm-2 control-label">文本</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-text" style="width: 200%;">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-orderNo" class="col-sm-2 control-label">排序号</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-orderNo" style="width: 200%;">
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="saveBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 修改字典值的模态窗口 -->
<div class="modal fade" id="editDicValueModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel2">修改字典值</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <input type="hidden" id="edit-id"/>
                    <div class="form-group">
                        <label for="edit-typeCode" class="col-sm-2 control-label">字典类型编码</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-typeCode" style="width: 200%;" value="性别"
                                   readonly>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-value" class="col-sm-2 control-label">字典值<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-value" style="width: 200%;" value="m">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-text" class="col-sm-2 control-label">文本</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-text" style="width: 200%;" value="男">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-orderNo" class="col-sm-2 control-label">排序号</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-orderNo" style="width: 200%;" value="1">
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="updateBtn">更新</button>
            </div>
        </div>
    </div>
</div>

<div>
    <div style="position: relative; left: 30px; top: -10px;">
        <div class="page-header">
            <h3>字典值列表</h3>
        </div>
    </div>
</div>
<div class="btn-toolbar" role="toolbar" style="background-color: #F7F7F7; height: 50px; position: relative;left: 30px;">
    <div class="btn-group" style="position: relative; top: 18%;">
        <%--data-toggle="modal" data-target="#createDicValueModal"--%>
        <button type="button" class="btn btn-primary" id="addBtn"><span class="glyphicon glyphicon-plus"></span> 创建
        </button>
        <button type="button" class="btn btn-default" id="editBtn"><span class="glyphicon glyphicon-edit"></span> 编辑
        </button>
        <button type="button" class="btn btn-danger" id="deleteBtn"><span class="glyphicon glyphicon-minus"></span> 删除
        </button>
    </div>
</div>
<div style="position: relative; left: 30px; top: 20px;">
    <table class="table table-hover">
        <thead>
        <tr style="color: #B3B3B3;">
            <input type="hidden" value="${typeCode2}" id="typeCode2">
            <td><input type="checkbox" id="qx"/></td>
            <td>序号</td>
            <td>字典值</td>
            <td>文本</td>
            <td>排序号</td>
            <td>字典类型编码</td>
        </tr>
        </thead>
        <tbody id="dicValueBody">
        <input type="hidden" value="${flag}" id="flag">
        <c:forEach items="${typeCode}" var="tc" varStatus="status">
            <tr class="active">
                <td><input type="checkbox" name="xz" value="${tc.id}"/></td>
                <td>${requestScope.offset+status.index+1}</td>
                <td>${tc.value}</td>
                <td>${tc.text}</td>
                <td>${tc.orderNo}</td>
                <td>${tc.typeCode}</td>
            </tr>
        </c:forEach>

        </tbody>
    </table>
</div>

</body>
</html>